package org.garbagecan.xstreamstudy.converter;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.thoughtworks.xstream.XStream;
import com.thoughtworks.xstream.io.xml.DomDriver;

public class TestMapListConverter {
	@SuppressWarnings({ "rawtypes", "unchecked" })
	public static void main(String[] args) {
		XStream xstream = new XStream(new DomDriver());
		Map map = new HashMap();
		
		List list1 = new ArrayList();
		list1.add(new T("a1", "b1", "c1"));
		list1.add(new T("a2", "b2", "c2"));
		List list2 = new ArrayList();
		list2.add(new T("a3", "b3", "c3"));
		list2.add(new T("a4", "b4", "c4"));
		map.put("t1", list1);
		map.put("t2", list2);
		
		xstream.alias("classes", Map.class);
		xstream.alias("class", Map.Entry.class);
		xstream.alias("name", String.class);
		
		xstream.alias("fields", List.class);
		xstream.alias("field", T.class);
		xstream.aliasAttribute(T.class, "a", "a");
		xstream.aliasAttribute(T.class, "b", "b");
		xstream.aliasAttribute(T.class, "c", "c");
		
		System.out.println(xstream.toXML(map));

		System.out.println((Map)xstream.fromXML(xstream.toXML(map)));
	}
}

class T {
	public T(String a, String b, String c) {
		this.a = a;
		this.b = b;
		this.c = c;
	}
	private String a;
	private String b;
	private String c;
	public String getA() {
		return a;
	}
	public void setA(String a) {
		this.a = a;
	}
	public String getB() {
		return b;
	}
	public void setB(String b) {
		this.b = b;
	}
	public String getC() {
		return c;
	}
	public void setC(String c) {
		this.c = c;
	}
	@Override
	public String toString() {
		return org.apache.commons.lang.builder.ToStringBuilder.reflectionToString(this);
	}
}
